package tinyinventor.compiler;

public class NescComponent extends Component {

	private String usesSingleton;
	private boolean hasVariable;
	private String rawCode;
	private String rawWiringCode;
	private String rawInterfaceCode;
	private String rawInitCode;
	private String rawDeclarationCode;
	private String rawModuleCode;
	
	public NescComponent(String name, String usesSingleton, boolean hasVariable, String rawCode, String rawWiringCode, String rawInterfaceCode, String rawInitCode, String rawDeclarationCode, String rawModuleCode) {
		super(name);
		this.usesSingleton = usesSingleton;
		this.hasVariable = hasVariable;
		this.rawCode = rawCode;
		this.rawWiringCode = rawWiringCode;
		this.rawInterfaceCode = rawInterfaceCode;
		this.rawInitCode = rawInitCode;
		this.rawDeclarationCode = rawDeclarationCode;
		this.rawModuleCode = rawModuleCode;
	}
	
	public String getUsesSingleton() {
		return usesSingleton;
	}
	
	public boolean hasVariable() {
		return hasVariable;
	}

	@Override
	protected String substituteVars(String code) {
		String result = super.substituteVars(code);
		result = result.replace("##MODULE##", NescCompiler.MODULE_NAME);
		result = result.replace("##STACK_SIZE##", String.valueOf(NescCompiler.STACK_SIZE));
		return result;
	}
	
	/**
	 * Get code for the component
	 * @return the code
	 */
	@Override
	public String getCode() {
		return substituteVars(rawCode);
	}
	
	/**
	 * Get wiring code for the component
	 * @return the code
	 */
	public String getWiringCode() {
		return substituteVars(rawWiringCode);
	}
	
	/**
	 * Get interface code for the component
	 * @return the code
	 */
	public String getInterfaceCode() {
		return substituteVars(rawInterfaceCode);
	}
	
	/**
	 * Get init code for the component
	 * @return the code
	 */
	public String getInitCode() {
		return substituteVars(rawInitCode);
	}

	/**
	 * Get declaration code for the component
	 * @return the code
	 */
	public String getDeclarationCode() {
		return substituteVars(rawDeclarationCode);
	}
	/**
	 * Get module code for the component
	 * @return the code
	 */
	public String getModuleCode() {
		return substituteVars(rawModuleCode);
	}
	
	/**
	 * Clone a component. This is an important method as all components
	 * are clones of prototypeComponents kept in the Compiler.
	 */
	@Override
	public NescComponent clone() {
		return new NescComponent(getName(), usesSingleton, hasVariable, rawCode, rawWiringCode, rawInterfaceCode, rawInitCode, rawDeclarationCode, rawModuleCode);
	}
	
	/**
	 * String representation of a component.
	 */
	@Override
	public String toString() {
		return "["+getClass().getSimpleName()+" " + getName() + (usesSingleton!=null?" includes "+usesSingleton:"") + (hasVariable?" variable":"") + "]";
	}

	
}
